package com.mdd.admin.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.admin.service.goods.ILimitedTimeGoodsService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.goods.LimitedTimeGoodsParam;
import com.mdd.admin.vo.goods.LimitedTimeGoodsDetailVo;
import com.mdd.admin.vo.goods.LimitedTimeGoodsListVo;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.goods.Goods;
import com.mdd.common.entity.goods.LimitedTimeGoods;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.goods.LimitedTimeGoodsMapper;
import com.mdd.common.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 限时商品实现类
 */
@Service
public class LimitedTimeGoodsServiceImpl extends ServiceImpl<LimitedTimeGoodsMapper, LimitedTimeGoods> implements ILimitedTimeGoodsService {

	@Resource
	private GoodsMapper goodsMapper;

	/**
	 * 限时商品列表
	 *
	 * @param pageParam 分页参数
	 * @param params    搜索参数
	 * @return PageResult<LimitedTimeGoodsListVo>
	 */
	@Override
	public PageResult<LimitedTimeGoodsListVo> list(PageParam pageParam, Map<String, String> params) {
		Integer page = pageParam.getPageNo();
		Integer limit = pageParam.getPageSize();

		QueryWrapper<LimitedTimeGoods> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_delete", 0);
		queryWrapper.orderByDesc("id");

		this.baseMapper.setSearch(queryWrapper, params, new String[]{
				"=:goodsId@goods_id:int",
				"=:price:str",
				"=:limitedGoodsNum@limited_goods_num:int",
				"=:limitedStartTime@limited_start_time:int",
				"=:limitedEndTime@limited_end_time:int",
		});

		IPage<LimitedTimeGoods> iPage = this.baseMapper.selectPage(new Page<>(page, limit), queryWrapper);

		List<LimitedTimeGoodsListVo> list = new LinkedList<>();
		for (LimitedTimeGoods item : iPage.getRecords()) {
			Goods goods = goodsMapper.selectById(item.getGoodsId());
			LimitedTimeGoodsListVo vo = new LimitedTimeGoodsListVo();
			BeanUtils.copyProperties(item, vo);
			vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
			vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
			vo.setGoodsName(goods.getName());
			vo.setBeforePrice(goods.getPrice());
			vo.setLimitedStartTime(TimeUtil.timestampToDate(Long.valueOf(item.getLimitedStartTime())));
			vo.setLimitedEndTime(TimeUtil.timestampToDate(Long.valueOf(item.getLimitedEndTime())));
			list.add(vo);
		}

		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
	}

	/**
	 * 限时商品详情
	 *
	 * @param id 主键参数
	 * @return LimitedTimeGoods
	 */
	@Override
	public LimitedTimeGoodsDetailVo detail(Integer id) {
		LimitedTimeGoods model = this.baseMapper.selectOne(
				new QueryWrapper<LimitedTimeGoods>()
						.eq("id", id)
						.eq("is_delete", 0)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在");

		LimitedTimeGoodsDetailVo vo = new LimitedTimeGoodsDetailVo();
		BeanUtils.copyProperties(model, vo);
		vo.setLimitedStartTime(TimeUtil.timestampToDate(Long.valueOf(model.getLimitedStartTime())));
		vo.setLimitedEndTime(TimeUtil.timestampToDate(Long.valueOf(model.getLimitedEndTime())));
		return vo;
	}

	/**
	 * 限时商品新增
	 *
	 * @param limitedTimeGoodsParam 参数
	 */
	@Override
	public void add(LimitedTimeGoodsParam limitedTimeGoodsParam) {
		Assert.notNull(goodsMapper.selectById(limitedTimeGoodsParam.getGoodsId()), "商品不存在");
		LimitedTimeGoods model = new LimitedTimeGoods();
		model.setGoodsId(limitedTimeGoodsParam.getGoodsId());
		model.setPrice(limitedTimeGoodsParam.getPrice());
		model.setLimitedGoodsNum(limitedTimeGoodsParam.getLimitedGoodsNum());
		model.setLimitedStartTime(Math.toIntExact(TimeUtil.dateToTimestamp(limitedTimeGoodsParam.getLimitedStartTime())));
		model.setLimitedEndTime(Math.toIntExact(TimeUtil.dateToTimestamp(limitedTimeGoodsParam.getLimitedEndTime())));
		model.setCreateTime(TimeUtil.timestamp());
		model.setUpdateTime(TimeUtil.timestamp());
		model.setIsDelete(GlobalConstant.NOT_DELETE);
		this.baseMapper.insert(model);
	}

	/**
	 * 限时商品编辑
	 *
	 * @param limitedTimeGoodsParam 参数
	 */
	@Override
	public void edit(LimitedTimeGoodsParam limitedTimeGoodsParam) {
		LimitedTimeGoods model = this.baseMapper.selectOne(
				new QueryWrapper<LimitedTimeGoods>()
						.eq("id", limitedTimeGoodsParam.getId())
						.eq("is_delete", 0)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在!");

		model.setId(limitedTimeGoodsParam.getId());
		model.setGoodsId(limitedTimeGoodsParam.getGoodsId());
		model.setPrice(limitedTimeGoodsParam.getPrice());
		model.setLimitedGoodsNum(limitedTimeGoodsParam.getLimitedGoodsNum());
		model.setLimitedStartTime(Math.toIntExact(TimeUtil.dateToTimestamp(limitedTimeGoodsParam.getLimitedStartTime())));
		model.setLimitedEndTime(Math.toIntExact(TimeUtil.dateToTimestamp(limitedTimeGoodsParam.getLimitedEndTime())));
		this.baseMapper.updateById(model);
	}

	/**
	 * 限时商品删除
	 *
	 * @param id 主键ID
	 */
	@Override
	public void del(Integer id) {
		LimitedTimeGoods model = this.baseMapper.selectOne(
				new QueryWrapper<LimitedTimeGoods>()
						.eq("id", id)
						.eq("is_delete", 0)
						.last("limit 1"));

		Assert.notNull(model, "数据不存在!");

		model.setIsDelete(1);
		model.setDeleteTime(System.currentTimeMillis() / 1000);
		this.baseMapper.updateById(model);
	}

}
